package com.jhx.demos.web.controller;

import com.jhx.demos.web.pojo.Result;
import com.jhx.demos.web.pojo.Admin;
import com.jhx.demos.web.pojo.Message;
import com.jhx.demos.web.service.adminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
public class adminController {
    @Autowired
    private adminService adminservice;

    @GetMapping
    public Result list() {
        log.info("查询全部的管理员表数据");

        //调用service查询数据
        List<Admin> deptList = adminservice.list();

        //响应一个成功的结果
        return Result.success(deptList);//返回回去的数据
    }

    @PostMapping
    public Result add(@RequestBody Admin adm) {
        log.info("添加管理员{}", adm);
        adminservice.add(adm);

        return Result.success(adm);
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除管理员{}" + id);
        adminservice.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result find(@PathVariable Integer id) {//这是路径参数要加@PathVariable
        log.info("根据id查询管理员表信息 ,id: {}", id);
        Message mes = adminservice.find(id);//将id传进来根据id查询员工信息
        return Result.success(mes);
    }

    //传入管理员名字admName修改管理员权限
    @PutMapping
    public Result update(@RequestBody Admin adm) {
        log.info("给管理员修改账号和权限:{}", adm);
        adminservice.update(adm);
        return Result.success();
    }

}
